#include "GameState.h"
#include "FinishLevelScreen.h"

int MainGameScreen::score =0;
int MainGameScreen::coin = 0;
int MainGameScreen::life = 3;
float MainGameScreen::time = 300;
bool MainGameScreen::hasBonus = true;
MainGameScreen::MainGameScreen() : IGameState()
{
	loadMap = new CLoadMap("MapMario\\lv1\\mapObjects.txt","MapMario\\lv1\\map.txt");
	quadtree = new CQuadtree();
	loadMap->LoadToQuadtree(quadtree);
	mapWidth = loadMap->mapWidth;
	mario = new Mario(D3DXVECTOR2(150,350),EMarioState::Esmall);
	camera = new CCamera();
	backGround = new CSprite(ResourceManager::GetIns()->GetBackGround(MainGameBG1));
	SoundManager::GetInst()->RemoveAllBGM();
	SoundManager::GetInst()->PlayBGSound(EBGSound::EMainGameSound1);
	level = 1;
	timeWait = 0;
	changeToBonusMap = false;
	m_text = new CText();
}

MainGameScreen::MainGameScreen(SaveGameInfor data) : IGameState()
{	
	level = data._level;
	loadMap=data._cloadMap;	
	quadtree=new CQuadtree();
	loadMap->LoadToQuadtree(quadtree);
	mapWidth = loadMap->mapWidth;
	mario=data._mario;
	mario->SetLife(data._life);
	mario->SetCoin(data._money);
	mario->SetScore(data._score);
	camera = new CCamera();	
	
	switch(level)
	{
	case 1:
		backGround = new CSprite(ResourceManager::GetIns()->GetBackGround(MainGameBG1));
		SoundManager::GetInst()->RemoveAllBGM();
		SoundManager::GetInst()->PlayBGSound(EBGSound::EMainGameSound1);
		break;
	case 2:
		backGround = new CSprite(ResourceManager::GetIns()->GetBackGround(Black));
		SoundManager::GetInst()->RemoveAllBGM();
		SoundManager::GetInst()->PlayBGSound(EBGSound::EMainGameSound2);
		break;
	case 3:
		backGround = new CSprite(ResourceManager::GetIns()->GetBackGround(MainGameBG1));
		SoundManager::GetInst()->RemoveAllBGM();
		SoundManager::GetInst()->PlayBGSound(EBGSound::EMainGameSound1);
		break;
	case 0: //bonus
		backGround = new CSprite(ResourceManager::GetIns()->GetBackGround(Black));
		this->mario->SetPosition(D3DXVECTOR2(100,300));
		break;
	}
	timeWait = 0;
	changeToBonusMap = false;
	m_text = new CText();
}

MainGameScreen::MainGameScreen(Mario* mario,int level) : IGameState()
{
	this->mario = mario;
	switch(level)
	{
	case 1:
		loadMap = new CLoadMap("MapMario\\lv1\\mapObjects.txt","MapMario\\lv1\\map.txt");
		backGround = new CSprite(ResourceManager::GetIns()->GetBackGround(MainGameBG1));
		SoundManager::GetInst()->RemoveAllBGM();
		SoundManager::GetInst()->PlayBGSound(EBGSound::EMainGameSound1);
		break;
	case 2:
		loadMap = new CLoadMap("MapMario\\lv2\\mapObjects.txt","MapMario\\lv2\\map.txt");
		backGround = new CSprite(ResourceManager::GetIns()->GetBackGround(Black));
		SoundManager::GetInst()->RemoveAllBGM();
		SoundManager::GetInst()->PlayBGSound(EBGSound::EMainGameSound2);
		break;
	case 3:
		loadMap = new CLoadMap("MapMario\\lv3\\mapObjects.txt","MapMario\\lv3\\map.txt");
		backGround = new CSprite(ResourceManager::GetIns()->GetBackGround(MainGameBG1));
		SoundManager::GetInst()->RemoveAllBGM();
		SoundManager::GetInst()->PlayBGSound(EBGSound::EMainGameSound1);
		break;
	case 0: //bonus
		loadMap = new CLoadMap("MapMario\\lvbonus\\mapObjects.txt","MapMario\\lvbonus\\map.txt");
		backGround = new CSprite(ResourceManager::GetIns()->GetBackGround(Black));
		this->mario->SetPosition(D3DXVECTOR2(100,300));
		break;
	}

	this->level = level;
	quadtree = new CQuadtree();
	loadMap->LoadToQuadtree(quadtree);
	mapWidth = loadMap->mapWidth;
	
	camera = new CCamera();

	m_text = new CText();
	timeWait = 0;
	changeToBonusMap = false;
}

MainGameScreen::~MainGameScreen()
{

}

void MainGameScreen::Create()
{

}

void MainGameScreen::Release()
{
	_listObjectsInViewport.clear();
}

void MainGameScreen::HandleInput(CInput* input,float dt)
{
	if(mario->GetObjectState()==EState::Die)
		return;
	if(input->IsKeyDown(DIK_RIGHT))
	{
		mario->TurnRight();
	}
	else
		//If arrow key left is pressing
		if(input->IsKeyDown(DIK_LEFT))
		{
			mario->TurnLeft();
		}
		else if(mario->GetObjectState()!=EShooting)
			mario->Stop();
	//If arrow key down is pressing
	if(input->IsKeyDown(DIK_DOWN))
	{
		if(mario->GetIsInPipe()==false)
		{
			mario->Sit();
		}
		else if(hasBonus==true)
		{
			changeToBonusMap = true;
		}
	}
	//If arrow key up is pressing
	if(input->IsKeyDown(DIK_UP))
	{
		//mario->Jump();
	}
	if(input->IsKeyPress(DIK_SPACE))
	{
		mario->Jump();
		SoundManager::GetInst()->PlaySoundEffect(ES_MarioJump);
	}
	if(input->IsKeyPress(DIK_P))
	{		
		// get information in case save
		SaveGameInfor data;
		data._level=level;
		data._life=life;
		data._money=coin;
		data._score=score;
		data._mario=this->mario;
		data._cloadMap=this->loadMap;
		data._tree=this->quadtree;
		data._listCreatingAfter=GameObject::_creatingAfterObjects;
		StateManager::GetInstance()->AddScreen(new PauseScreen(data));
	}


	if(input->IsKeyPress(DIK_C))
	{
		StateManager::GetInstance()->AddScreen(new MainGameScreen(mario,0));
	}

	if(input->IsKeyPress(DIK_Q))
	{
		ExitSceen();
		StateManager::GetInstance()->AddScreen(new FinishLevelScreen(mario,level));
		
	}

	if(input->IsKeyPress(DIK_D))
	{
		ExitSceen();
		mario->SetPosition(D3DXVECTOR2(100,400));
	}
	if(input->IsKeyPress(DIK_Z))
	{
		mario->Shoot();
	}

}

void MainGameScreen::Update(float dt)
{
	//Update cac thong so trong game
	time -= dt;
	if(mario->GetScore()<score)
		mario->SetScore(score);
	if(mario->GetCoin() < coin)
		mario->SetCoin(coin);
	coin = mario->GetCoin();
	life = mario->GetLife();
	score = mario->GetScore();

	//Insert object moi sinh ra
	if(GameObject::_creatingAfterObjects.size()>0)
	{
		quadtree->Insert(GameObject::_creatingAfterObjects);
		GameObject::_creatingAfterObjects.clear();
	}
	//Lay danh sach cac doi tuong trong man hinh
	_listObjectsInViewport = quadtree->GetObjectsInViewport(camera->GetBound());

	list<GameObject*>::iterator i,j;

	mario->Update(dt);

	//Chui xuong cong
	if(changeToBonusMap==false)
	{
		list<GameObject*> tmpList = quadtree->GetObjectsInViewport(mario->GetBoundCheckCollision());
		mario->UpdateCollision(tmpList,dt);
	}
	else
	{
		mario->Fall();
		mario->SetVelocity(D3DXVECTOR2(0,-1));
		if(mario->GetPosition().y < 50)
		{
			changeToBonusMap = false;
			StateManager::GetInstance()->AddScreen(new MainGameScreen(mario,0));
		}
	}

	for (i = _listObjectsInViewport.begin(); i != _listObjectsInViewport.end(); i++)
	{
		if((*i)->GetObjectState()!=EState::Die)
		{
			(*i)->Update(dt);
			if((*i)->GetKindOfObject()==EDynamic)
			{
				list<GameObject*> tmpList2 = quadtree->GetObjectsInViewport((*i)->GetBoundCheckCollision());
				(*i)->UpdateCollision(tmpList2,dt);
			}
		}
	}	

	camera->Update(mario->GetPosition(),mapWidth);
	//Gioi han vi tri cua mario
	int posy = mario->GetPosition().y;
	if(mario->GetPosition().x < camera->GetPosition().x)
	{
		mario->SetPosition(D3DXVECTOR2(camera->GetPosition().x, posy));
	}
	if(mario->GetPosition().x > mapWidth - 50)
		mario->SetPosition(D3DXVECTOR2(mapWidth-50,mario->GetPosition().y));

	//Thoat khoi BonusMap
	if(level==0)
	{
		if(mario->GetPosition().x > mapWidth - 150 && mario->GetPosition().y < 300)
		{
			timeWait+=dt;
			if(timeWait>1)
			{
				if(StateManager::GetInstance()->GetListState().size()>1)
				{
					ExitSceen();
					mario->SetPosition(D3DXVECTOR2(2700,200));
					hasBonus = false;
				}
				else
				{
					ExitSceen();
					mario->SetPosition(D3DXVECTOR2(2700,200));
					StateManager::GetInstance()->AddScreen(new MainGameScreen(mario,1));	
					hasBonus = false;
				}
			}
		}
	}

	//Qua man
	if(mario->isLevelFinish==true)
	{
		timeWait+=dt;
		SoundManager::GetInst()->StopAllBGSound();
		SoundManager::GetInst()->PlaySoundEffect(ESoundEffect::ES_Win);
		if(timeWait>3)
		{
			timeWait = 0;
			ExitSceen();
			StateManager::GetInstance()->AddScreen(new FinishLevelScreen(mario,level));
		}
	}
	//Thua
	if(life <= 0)
	{
		SoundManager::GetInst()->StopAllBGSound();
		SoundManager::GetInst()->PlaySoundEffect(ESoundEffect::ES_Lose);
		timeWait+=dt;
		if(timeWait>3)
		{
			timeWait = 0;
			ExitSceen();
			mario->SetMarioState(Esmall);
			StateManager::GetInstance()->AddScreen(new FinishLevelScreen(mario,level));
		}
	}

}

void MainGameScreen::Draw()
{
	D3DXMATRIX matrix;
	D3DXMatrixIdentity(&matrix);
	GL_graphic->m_pSpriteHandler->SetTransform(&matrix);
	backGround->Draw(D3DXVECTOR2(400,300),SpriteEffect::Vertical,0,20);
	GL_graphic->m_pSpriteHandler->SetTransform(&camera->GetTransform());
	
	list<GameObject*>::iterator i;

	for (i = _listObjectsInViewport.begin(); i != _listObjectsInViewport.end(); i++)
	{
		if((*i)!=NULL &&(*i)->GetKindOfObject()==EScene)			
			(*i)->Draw();
	}
	mario->Draw();
	for (i = _listObjectsInViewport.begin(); i != _listObjectsInViewport.end(); i++)
	{
		if((*i)!=NULL && (*i)->GetKindOfObject()!=EScene)			
			(*i)->Draw();
	}

	DrawString();
}

void MainGameScreen::DrawString()
{
	RECT m_textPosition;
	char* _text = new char;
	LPCSTR _p;
	//Life
	SetRect(&m_textPosition,100,10,200,790);
	m_text->Draw("Life X ",m_textPosition,DT_LEFT,D3DCOLOR_XRGB(255,255,255));
	SetRect(&m_textPosition,170,10,200,790);
	itoa(life,_text,10);
	_p = _text;
	m_text->Draw(_p,m_textPosition,DT_LEFT,D3DCOLOR_XRGB(255,0,0));

	//Score
	SetRect(&m_textPosition,20,10,200,790);
	itoa(score,_text,10);
	_p = _text;
	m_text->Draw(_p,m_textPosition,DT_LEFT,D3DCOLOR_XRGB(255,255,0));
	
	//Coin
	SetRect(&m_textPosition,200,10,400,790);
	m_text->Draw("Coin X",m_textPosition,DT_CENTER,D3DCOLOR_XRGB(255,255,255));
	SetRect(&m_textPosition,300,10,400,790);
	itoa(coin,_text,10);
	_p = _text;
	m_text->Draw(_p,m_textPosition,DT_CENTER,D3DCOLOR_XRGB(255,0,0));

	//Level
	SetRect(&m_textPosition,400,10,600,790);
	m_text->Draw("WORLD: ",m_textPosition,DT_CENTER,D3DCOLOR_XRGB(255,255,255));
	SetRect(&m_textPosition,530,10,600,790);
	itoa(level,_text,10);
	_p = _text;
	m_text->Draw(_p,m_textPosition,DT_CENTER,D3DCOLOR_XRGB(255,0,0));

	//Time
	SetRect(&m_textPosition,600,10,800,790);
	m_text->Draw("Times: ",m_textPosition,DT_CENTER,D3DCOLOR_XRGB(255,255,255));
	SetRect(&m_textPosition,730,10,800,790);
	itoa(time,_text,10);
	_p = _text;
	m_text->Draw(_p,m_textPosition,DT_CENTER,D3DCOLOR_XRGB(255,0,0));
}